package cn.abcsys.cloud.devops.runtime.controller;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.abcsys.cloud.devops.runtime.bean.GridBean;
import cn.abcsys.cloud.devops.runtime.bean.Result;
import cn.abcsys.cloud.devops.runtime.entity.Registry;
import cn.abcsys.cloud.devops.runtime.model.RegistryModel;
import cn.abcsys.cloud.devops.runtime.service.RegistryService;
import net.sf.json.JSONObject;


/**
 * 集群管理控制器
 * @author mayunhao
 *
 */
@RestController
@RequestMapping("/registry")
public class RegistryController {
	private static final Logger LOGGER = Logger.getLogger(RegistryController.class);
	@Autowired
	private RegistryService registryService;
	@RequestMapping(value = "/list", method = { RequestMethod.GET })
	@ResponseBody
	public GridBean clusterList(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return null;
		}else{
			RegistryModel registryModel = new RegistryModel();
			JSONObject orig = JSONObject.fromObject(params);
			registryModel = (RegistryModel) JSONObject.toBean(orig,RegistryModel.class);
			return registryService.getRegistryList(registryModel);
		}
	}
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	@ResponseBody
	public Result getDetail(@RequestParam(value = "registryId", required = true)Integer registryId){
		if(null==registryId){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}
		return registryService.getRegistryDetail(registryId);
	}
	@RequestMapping(value = "/create", method = { RequestMethod.GET })
	@ResponseBody
	public Result create(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Registry registry = new Registry();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				registry = (Registry) JSONObject.toBean(orig,Registry.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return registryService.createRegistry(registry);
		}
	}
	@RequestMapping(value = "/modify", method = { RequestMethod.GET })
	@ResponseBody
	public Result modify(@RequestParam Map<String,Object> params){
		if(params.isEmpty()){
			LOGGER.error("请求参数为空");
			return new Result(false, "请求参数为空");
		}else{
			Registry registry = new Registry();
			try{
				JSONObject orig = JSONObject.fromObject(params);
				registry = (Registry) JSONObject.toBean(orig,Registry.class);
			}catch(Exception e){
				LOGGER.error("请求参数转换错误："+e.getMessage());
				return new Result(false, "请求参数转换错误："+e.getMessage());
			}
			return registryService.updateRegistry(registry);
		}
	}
	@RequestMapping(value = "/remove", method = { RequestMethod.GET })
	@ResponseBody
	public Result remove(@RequestParam(value = "registryId", required = true)Integer registryId){
		return registryService.deleteRegistry(registryId);
	}
	@RequestMapping(value = "/listAll", method = { RequestMethod.GET })
	@ResponseBody
	public Result listAll(){
		return registryService.listAll();
	}
}
